package com.itheima.itheima_health.service.Impl;

import com.fasterxml.jackson.databind.JsonSerializable;
import com.itheima.itheima_health.constant.MessageConstant;
import com.itheima.itheima_health.constant.RedisConstant;
import com.itheima.itheima_health.exception.BaseException;
import com.itheima.itheima_health.mapper.OrderMapper;
import com.itheima.itheima_health.pojo.Member;
import com.itheima.itheima_health.pojo.Order;
import com.itheima.itheima_health.pojo.OrderSetting;
import com.itheima.itheima_health.pojo.User;
import com.itheima.itheima_health.service.OrderService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Transactional
    @Override
    public Integer SubmitOrder(Map ordermap) {
        Object orediscode = redisTemplate.opsForSet().randomMember(ordermap.get("email") + RedisConstant.EMAIL_CODE_KEY);
        if (!ordermap.get("code").equals(orediscode)){
            log.info("验证码无效");
            throw new BaseException(MessageConstant.LOGIN_CODE_FAIL1);
        }
        //查询当前日期是否已经设置了预约
        Integer count = orderMapper.findByOrderDate(ordermap);
        if (count<1){
            log.info("当前日期不能预约");
            throw new BaseException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //查询当前日期是否预约满

        OrderSetting orderSetting = orderMapper.getOrderPeople(ordermap);
        if(orderSetting==null){
            log.info("当前日期预约已满");
            throw new BaseException(MessageConstant.ORDER_FULL);
        }
        //查询用户是否重复预约
        Member member = orderMapper.findByEmail(ordermap);
        if (member!=null){
            log.info("会员存在,无需要注册");
            Integer memberId = member.getId();
            ordermap.put("memberId",memberId);
            log.info("map:{}",ordermap);
            Integer count3 = orderMapper.findByCondition(ordermap);

            if (count3>0){
                log.info("重复预约");
                throw new BaseException(MessageConstant.HAS_ORDERED);
            }
        }else {
            log.info("会员不存在,自动注册");
                    member = Member
                    .builder()
                    .name((String) ordermap.get("name"))
                    .sex((String) ordermap.get("sex"))
                    .idCard((String) ordermap.get("idCard"))
                    .regTime(LocalDate.now())
                    .password("123456").email((String) ordermap.get("email"))
                    .build();
            orderMapper.addvip(member);
        }
        //添加预约信息
        Order order = Order
                .builder()
                .memberId(member.getId())
                .orderDate(LocalDate.now().plusDays(1))
                .orderType(Order.ORDERTYPE_EMAIL)
                .orderStatus(Order.ORDERSTATUS_NO)
                .setmealId(Integer.valueOf(ordermap.get("setmealId").toString()))  // 转换为 Integer
                .build();

        log.info("添加预约消息:{}",order);
        log.info("预约信息修改前:{}",orderSetting);
        orderMapper.addOrder(order);
        //更新  可预约数-1  预约数+1
        orderSetting.setNumber(orderSetting.getNumber()-1);
        orderSetting.setReservations(orderSetting.getReservations()+1);
        log.info("预约信息添加:{}",orderSetting);
        orderMapper.updateOrderSetting(orderSetting);

        return order.getId();
    }

    @Override
    public Map findById(Integer id) {
        Map map = orderMapper.findById4Detail(id);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",orderDate);
        }
        return map;
    }
}
